from django.db import models
from django.utils import timezone
from datetime import timedelta


class Question(models.Model):
    question_text = models.CharField(max_length=512)
    pub_date = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return '<Question: %s>' % self.question_text

    def __repr__(self):
        return self.__str__()

    def was_published_recently(self):
        if not self.pub_date:
            return False
        # return timezone.now() - self.pub_date < timedelta(days=1)
        now = timezone.now()
        return   now - timedelta(days=1) < self.pub_date < now


class Choice(models.Model):
    choice_text = models.CharField(max_length=128)
    votes = models.IntegerField(default=0)
    question = models.ForeignKey(Question)

    def __str__(self):
        return self.choice_text

    def __repr__(self):
        return self.__str__()


class User(models.Model):
    name = models.CharField(max_length=8)
    password = models.CharField(max_length=32)
